package com.beta.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.beta.entity.Shipments;
import com.beta.service.ServiceException;
import com.beta.dto.admin.CourierDto;
import com.beta.em.admins.CourierStatusType;
import com.beta.entity.Cities;
import com.beta.service.ICitiesService;
import com.beta.service.IShipmentsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beta.mapper.CouriersMapper;
import com.beta.entity.Couriers;
import com.beta.service.ICouriersService;
import com.baomidou.framework.service.impl.SuperServiceImpl;

import java.util.List;
import java.util.Objects;

/**
 *
 * Couriers 表数据服务层接口实现类
 *
 */
@Service
public class CouriersService extends SuperServiceImpl<CouriersMapper, Couriers> implements ICouriersService {
    @Autowired
    private DateTimeService dbService;
    @Autowired
    private ICitiesService citiesService;
    @Autowired
    private IShipmentsService shipmentsService;
    @Override
    public Couriers pickCourier(String cityCode) {
        if (StringUtils.isBlank(cityCode)) {
            throw new ServiceException("E40009");
        }

        Couriers whereEntity = new Couriers().setCityCode(cityCode);
        List<Couriers> couriers = selectList(new EntityWrapper<>(whereEntity));
        return CollectionUtils.isEmpty(couriers) ? null : couriers.get(0);
    }

    @Override
    public Couriers getCourierById(Integer courierId) {
        return selectById(courierId);
    }

    @Override
    public void createCourier(CourierDto courierDto) {
        Cities cityEntity = citiesService.selectOne(new Cities().setCode(courierDto.getCityCode()));
        if(Objects.isNull(cityEntity)){
            throw new ServiceException("cities.invalid.city");
        }
        Couriers couriers = new Couriers().setName(courierDto.getName())
                .setPhone(courierDto.getPhone())
                .setContact(courierDto.getContact())
                .setStatus(courierDto.getStatus())
                .setCityCode(courierDto.getCityCode())
                .setCityName(cityEntity.getName())
                .setNotes(courierDto.getNotes())
                .setCreatedAt(dbService.toDbDate())
                .setUpdatedAt(dbService.toDbDate())
                .setCode(courierDto.getCode())
                .setEncryptedPassword(BCrypt.hashpw(courierDto.getPassword(), BCrypt.gensalt()));
        insertSelective(couriers);
    }

    @Override
    public void updateCourier(Integer courierId,CourierDto courierDto) {
        Cities cityEntity = citiesService.selectOne(new Cities().setCode(courierDto.getCityCode()));
        if(Objects.isNull(cityEntity)){
            throw new ServiceException("cities.invalid.city");
        }
        Couriers couriers = new Couriers().setName(courierDto.getName())
                .setPhone(courierDto.getPhone())
                .setContact(courierDto.getContact())
                .setStatus(courierDto.getStatus())
                .setCityCode(courierDto.getCityCode())
                .setCityName(cityEntity.getName())
                .setNotes(courierDto.getNotes())
                .setUpdatedAt(dbService.toDbDate())
                .setCode(courierDto.getCode())
                .setEncryptedPassword(BCrypt.hashpw(courierDto.getPassword(), BCrypt.gensalt()));
        updateSelective(couriers,new Couriers().setId(courierId));
    }

    @Override
    public Page<Couriers> getCouriers(Page<Couriers> page, String text) {
        List<Couriers> couriers = baseMapper.getCouriers(page, text);
        for (Couriers courier:couriers) {
            courier.setStatus(CourierStatusType.getCnValue(courier.getStatus()));
        }
        page.setRecords(couriers);
        return page;
    }

    @Override
    public void deleteCourier(Integer courierId) {
        if(Objects.isNull(courierId)){
            throw new ServiceException("couriers.invalid.id");
        }
        List<Shipments> shipments = shipmentsService.selectList(new EntityWrapper<>(new Shipments().setCourierId(courierId)));
        for (Shipments shipment:shipments) {
            if("shipping".equals(shipment.getStatus())){
                throw new ServiceException("couriers.related");
            }
        }
        deleteById(courierId);
    }
}